﻿#include "AssignBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

AssignBlock::AssignBlock(QString &name, QRect &position, double zoom, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
}

AssignBlock::~AssignBlock()
{

}

void AssignBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = 50 * zoom();
    double b = m_bScale * a;
    double bottom = y + height;

    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + m_lineScale * zoom()));
    drawBottomArrow(canvas, QPointF(hcenter, y + m_lineScale * zoom()),
                                QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));
    QRectF rect(hcenter - b/2, y + m_lineScale * zoom(), b, a);
    QRectF textRect(hcenter - b/2+4, y + m_lineScale * zoom()+4, b-8, a-8);

    QBrush brush = canvas->brush();
    canvas->setBrush(m_backgroundColor);
    canvas->drawRect(rect);
    canvas->setBrush(brush);

    UseOldPen

    QStringList li = name().split(":");
    if(li.size() != 2)
        return;
    canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1 := %2").arg(li.at(0),li.at(1)));

    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y + m_lineScale * zoom()+a, hcenter, bottom+0.5));
    UseOldPen
}

QString AssignBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><assign dest=\"x\" src=\"0\" id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\"/></branch></algorithm>")
            .arg(1).arg(2).arg(3).arg(4);
    return xmlStr;
}
}
}
